package handler

import (
	"6-11zk/api/response"
	"6-11zk/pkg"
	"github.com/gin-gonic/gin"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if file.Size > 1024*1024*5 {
		response.ResponseError(c, "文件过大")
		return
	}
	ext := filepath.Ext(file.Filename)
	m := map[string]bool{
		".png": true,
		".jpg": true,
		".mp4": true,
	}
	if !m[ext] {
		response.ResponseError(c, "类型错误")
		return
	}
	dst := "upload/" + ext
	c.SaveUploadedFile(file, dst)
	pkg.Upload(file.Filename, dst)
	response.ResponseSuccess(c, "上传成功"+pkg.GetUrl(file.Filename))
}
